package net.wangds.validatecode.comp;

import net.wangds.utils.ImageFormatEnum;
import net.wangds.utils.ImageUtils;
import net.wangds.validatecode.model.VerifyInfo;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.UUID;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/5/13 16:13.
 */
@Component
public class ValidateCodeComp {

    private static String baseNumLetter= "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

    private boolean drawLines = false;

        public VerifyInfo drawRandomText(int width, int height) {

            BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

            int chatWidth = (int)Math.ceil(((float)width)/5);
            int margin = chatWidth/2;
            int halfHeight = height/2;
            int fontSize = (int)(height*1.1);
            fontSize = height;
            int posHeight = height*7/8;

            Graphics2D graphics = (Graphics2D)verifyImg.getGraphics();

            graphics.setColor(Color.WHITE);//设置画笔颜色-验证码背景色

            graphics.fillRect(0, 0, width, height);//填充背景


            //数字和字母的组合




            StringBuffer sBuffer = new StringBuffer();

            int x = (margin+chatWidth)/2;  //旋转原点的 x 坐标

            String ch = "";

            Random random = new Random();



            graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, fontSize));
            for(int i = 0;i < 4;i++){

                //graphics.setColor(getRandomColor());
                graphics.setColor(new Color(184,136,41));

                //设置字体旋转角度

                int degree = random.nextInt() % 10;  //角度小于30度

                int dot = random.nextInt(baseNumLetter.length());

                ch = baseNumLetter.charAt(dot) + "";

                sBuffer.append(ch);

                //正向旋转

                graphics.rotate(degree * Math.PI / 180, x, halfHeight);

                graphics.drawString(ch, x, posHeight);

                //反向旋转

                graphics.rotate(-degree * Math.PI / 180, x, halfHeight);

                x += chatWidth;

            }

            //画干扰线
            if(drawLines) {
                for (int i = 0; i < 6; i++) {

                    // 设置随机颜色

                    graphics.setColor(getRandomColor());

                    // 随机画线

                    graphics.drawLine(random.nextInt(width), random.nextInt(height),

                            random.nextInt(width), random.nextInt(height));

                }
            }

            //添加噪点

            for(int i=0;i<10;i++){

                int x1 = random.nextInt(width);

                int y1 = random.nextInt(height);

                graphics.setColor(getRandomColor());

                graphics.fillRect(x1, y1, 2,2);

            }

            String code = sBuffer.toString();


            return VerifyInfo.builder().code(code).token(UUID.randomUUID().toString())
                    .img(ImageUtils.imgBytes2Blob(ImageUtils.toBytes(verifyImg, "png"), ImageFormatEnum.PNG))
                    .build();
        }

        /**
         * 随机取色
         */

        private static Color getRandomColor() {

            Random ran = new Random();

            Color color = new Color(ran.nextInt(256),

                    ran.nextInt(256), ran.nextInt(256));

            return color;

        }


}
